package database

import (
	"demo/models"
	"errors"

	"github.com/sirupsen/logrus"
)

//根据用户id获取所有通知
func GetElectricitynoticesByUserId(userId int) ([]models.Notifications, error) {
	var notices []models.Notifications
	err := engine.Where("user_id = ?", userId).Find(&notices)
	if err != nil {
		logrus.Error("failed to get bills: ", err)
		return nil, err
	}
	if len(notices) == 0 {
		return nil, errors.New("no notices found for the user")
	}
	return notices, nil

}

//获取通知根据用户通知id
func GetNoticeByNoticeId(noticeId int)(*models.Notifications,error){
	notice := &models.Notifications{
		Id: noticeId,
	}
	exist, err := engine.Where("id = ?", noticeId).Get(notice)
	if err != nil {
		logrus.Error("failed to get notice: ", err)
		return 	nil, err
	}
	if !exist {
		return nil, ErrNotFound
	}
	return 	notice,nil
}
//根据用户通知id更新通知状态
func UpdateNoticeStatus(notice *models.Notifications, cols ...string) error {
	_, err := engine.Where("id = ?", notice.Id).Cols(cols...).Update(notice)
	return err
}